package com.dq.context.config;


import com.dq.context.filter.MyTokenAuthcFilter;
import com.dq.shiro.RedisCacheManager;
import com.dq.shiro.UserRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * Created by 葛文镇
 * email:15258397904@163.com
 */
@Configuration
public class ShiroConfig {
    /**
     * redis缓存方案
     */
    @Bean
    public CacheManager shiroRedisCacheManager() {
        return new RedisCacheManager();
    }

    /**
     * 自定义Real
     */
    @Bean
    public UserRealm userRealm() {
        UserRealm realm = new UserRealm();
        realm.setCacheManager(shiroRedisCacheManager());
        return realm;
    }

    /***
     * 安全管理配置
     */
    @Bean
    public SecurityManager defaultWebSecurityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();// 注意:！！！初始化成这个将会报错java.lang.IllegalArgumentException:
        // 配置
        securityManager.setRealm(userRealm());
        // 根据情况选择缓存器
        securityManager.setCacheManager(shiroRedisCacheManager());//shiroEhCacheManager()
        // 注意这里必须配置securityManager
        SecurityUtils.setSecurityManager(securityManager);

        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();

        /**
         * 登录页面
         */
        factoryBean.setLoginUrl("/sys/login");
        /**
         * filter
         */
        factoryBean.getFilters().put("myTokenAuthc", new MyTokenAuthcFilter());
        /**
         * 过滤规则
         */
        Map definitions = factoryBean.getFilterChainDefinitionMap(); // attention // 这里是一个 LinkedHashMap
        /*********************merchant************************/

        // todo shiro
//        definitions.put("/sys/boss/**", "anon");
//        definitions.put("/sys/org/**", "anon");
        definitions.put("/**/nologin/**", "anon");


        //merchant_login
        definitions.put("/api/login/**", "anon");
        //staff login
        definitions.put("/app/login/**", "anon");
        //测试定时任务
        definitions.put("/api/test/**", "anon");
        //annex
        definitions.put("/annex/**", "anon");
        // assets
        definitions.put("/statics/**", "anon");
        definitions.put("/api/aboutUs/**", "anon");
        definitions.put("/sys/login/**", "anon");
        // swagger-ui
        definitions.put("/webjars/**", "anon");
        definitions.put("/swagger-resources/**", "anon");
        definitions.put("/v2/api-docs/**", "anon");
        definitions.put("/swagger-ui.html", "anon");
        // end assets
        // springcloud
        definitions.put("/actuator/**", "anon"); // 服务站点信息
        // end permissions

        definitions.put("/**", "myTokenAuthc");

        factoryBean.setSecurityManager(defaultWebSecurityManager());
        return factoryBean;
    }

}
